{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Minimum Time to Type Word Using Special Typewriter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minTimeToType"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #使用特殊打字机键入单词的最少时间"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>有一个特殊打字机，它由一个 <strong>圆盘</strong> 和一个 <strong>指针</strong>&nbsp;组成， 圆盘上标有小写英文字母&nbsp;<code>'a'</code> 到&nbsp;<code>'z'</code>。<strong>只有</strong>&nbsp;当指针指向某个字母时，它才能被键入。指针 <strong>初始时</strong>&nbsp;指向字符 <code>'a'</code>&nbsp;。</p>\n",
    "<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/07/31/chart.jpg\" style=\"width: 530px; height: 410px;\" />\n",
    "<p>每一秒钟，你可以执行以下操作之一：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>将指针 <strong>顺时针</strong>&nbsp;或者 <b>逆时针</b>&nbsp;移动一个字符。</li>\n",
    "\t<li>键入指针 <strong>当前</strong>&nbsp;指向的字符。</li>\n",
    "</ul>\n",
    "\n",
    "<p>给你一个字符串&nbsp;<code>word</code>&nbsp;，请你返回键入&nbsp;<code>word</code>&nbsp;所表示单词的 <b>最少</b>&nbsp;秒数&nbsp;。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>word = \"abc\"\n",
    "<b>输出：</b>5\n",
    "<strong>解释：\n",
    "</strong>单词按如下操作键入：\n",
    "- 花 1 秒键入字符 'a' in 1 ，因为指针初始指向 'a' ，故不需移动指针。\n",
    "- 花 1 秒将指针顺时针移到 'b' 。\n",
    "- 花 1 秒键入字符 'b' 。\n",
    "- 花 1 秒将指针顺时针移到 'c' 。\n",
    "- 花 1 秒键入字符 'c' 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>word = \"bza\"\n",
    "<b>输出：</b>7\n",
    "<strong>解释：\n",
    "</strong>单词按如下操作键入：\n",
    "- 花 1 秒将指针顺时针移到 'b' 。\n",
    "- 花 1 秒键入字符 'b' 。\n",
    "- 花 2 秒将指针逆时针移到 'z' 。\n",
    "- 花 1 秒键入字符 'z' 。\n",
    "- 花 1 秒将指针顺时针移到 'a' 。\n",
    "- 花 1 秒键入字符 'a' 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>word = \"zjpc\"\n",
    "<b>输出：</b>34\n",
    "<strong>解释：</strong>\n",
    "单词按如下操作键入：\n",
    "- 花 1 秒将指针逆时针移到 'z' 。\n",
    "- 花 1 秒键入字符 'z' 。\n",
    "- 花 10 秒将指针顺时针移到 'j' 。\n",
    "- 花 1 秒键入字符 'j' 。\n",
    "- 花 6 秒将指针顺时针移到 'p' 。\n",
    "- 花 1 秒键入字符 'p' 。\n",
    "- 花 13 秒将指针逆时针移到 'c' 。\n",
    "- 花 1 秒键入字符 'c' 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= word.length &lt;= 100</code></li>\n",
    "\t<li><code>word</code>&nbsp;只包含小写英文字母。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [minimum-time-to-type-word-using-special-typewriter](https://leetcode.cn/problems/minimum-time-to-type-word-using-special-typewriter/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [minimum-time-to-type-word-using-special-typewriter](https://leetcode.cn/problems/minimum-time-to-type-word-using-special-typewriter/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"abc\"', '\"bza\"', '\"zjpc\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        pre, cnt = 'a', len(word)\n",
    "        # 遍历每个字母\n",
    "        for w in word:\n",
    "            num = abs(ord(w) - ord(pre))\n",
    "            cnt += min(num, 26 - num)\n",
    "            pre = w\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        res, tmp = 0, 'a'\n",
    "        for i in word:\n",
    "            res += min(abs(ord(i)-ord(tmp)), 26-abs(ord(i)-ord(tmp))) + 1\n",
    "            tmp = i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans = len(word)\n",
    "        word1 = 'a'+word\n",
    "        for i in range(1,len(word1)):\n",
    "            ans += min(abs(ord(word1[i])-ord(word1[i-1])),26-abs(ord(word1[i])-ord(word1[i-1])))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        res, tmp = 0, 'a'\n",
    "        for i in word:\n",
    "            res += min(abs(ord(i)-ord(tmp)), 26-abs(ord(i)-ord(tmp))) + 1\n",
    "            tmp = i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        lst = [ord(c) - 97 for c in word]\n",
    "\n",
    "        pre = lst[0]\n",
    "        ans = min(pre, 26 - pre) + 1\n",
    "        for c in lst[1:]:\n",
    "            if c > pre:\n",
    "                a = c - pre\n",
    "                b = pre + 26 - c\n",
    "            else:\n",
    "                a = pre - c\n",
    "                b = c + 26 - pre\n",
    "            pre = c\n",
    "            ans += min(a, b) + 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans = len(word)\n",
    "        pre = \"a\"\n",
    "        for c in word:\n",
    "            a = abs(ord(c) - ord(pre))\n",
    "            ans += min(a, 26 - a)\n",
    "            pre = c\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "\n",
    "\n",
    "        res = 0\n",
    "        cur = 0\n",
    "\n",
    "        for c in word:\n",
    "            nex = ord(c) - ord('a')\n",
    "            res += min(abs(cur - nex), 26 - abs(cur - nex))  + 1\n",
    "            cur = nex\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        cnt_time = 0\n",
    "        tmp_w  = 'a'\n",
    "        for i in word:\n",
    "            tmp_dis = min( abs(ord(tmp_w) - ord(i)) , 26 -  abs(ord(tmp_w) - ord(i)) )\n",
    "            cnt_time += tmp_dis\n",
    "            cnt_time += 1 \n",
    "            tmp_w = i\n",
    "        return cnt_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans = 0\n",
    "        for c1, c2 in pairwise(chain('a', word)):\n",
    "            num = abs(ord(c2)  - ord(c1))\n",
    "            ans += min(num, 26 - num)\n",
    "        return ans + len(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def run_time(a:str, b:str) -> int:\n",
    "    na, nb = ord(a), ord(b)\n",
    "    if na > nb: \n",
    "        na, nb = nb, na\n",
    "    return min(nb-na, na-nb+26)\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        all_time = 0\n",
    "        current = \"a\"\n",
    "        for a in word:\n",
    "            if a!=current:\n",
    "                all_time += run_time(current, a)\n",
    "                current = a\n",
    "            all_time += 1\n",
    "        return all_time\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        res = len(word)\n",
    "        cur = \"a\"\n",
    "        for char in word: \n",
    "            val = abs(ord(char) - ord(cur))\n",
    "            res += min(val, 26 - val)\n",
    "            cur = char\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        prevPos = ord('a')\n",
    "        curTime = 0\n",
    "        for letter in word:\n",
    "            dist = abs(ord(letter) - prevPos)\n",
    "            if dist > 13:\n",
    "                dist = 26 - dist\n",
    "            curTime += dist + 1\n",
    "            prevPos = ord(letter)\n",
    "        \n",
    "        return curTime\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        prev=0\n",
    "        res=0\n",
    "        for ch in word:\n",
    "            curr =ord(ch)-ord('a')\n",
    "            res +=1 + min(abs(curr-prev),26-abs(curr-prev))\n",
    "            prev=curr\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        prev = 0\n",
    "        res = 0   # 当前位置\n",
    "        for ch in word:\n",
    "            # 计算键入每个字符的最小耗时并更新当前位置\n",
    "            curr = ord(ch) - ord('a')\n",
    "            res += 1 + min(abs(curr - prev), 26 - abs(curr - prev))\n",
    "            prev = curr\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        s_time = 0\n",
    "        word_list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']\n",
    "        for w in word:\n",
    "            w_i = word_list.index(w)\n",
    "            w_ri = 26 - w_i\n",
    "            m_step = min(w_i,w_ri)\n",
    "            m_flag = w_i - w_ri\n",
    "            if m_flag <= 0:\n",
    "                word_list = word_list[m_step:] + word_list[:m_step]\n",
    "                s_time += m_step\n",
    "            else:\n",
    "                word_list = word_list[-m_step:] + word_list[:-m_step]\n",
    "                s_time += m_step\n",
    "            s_time += 1\n",
    "        return s_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans = 0\n",
    "        prev = 'a'\n",
    "        for c in word:\n",
    "            x, y = prev, c\n",
    "            if x > y:\n",
    "                x, y = y, x\n",
    "            a = ord(y) - ord(x)\n",
    "            b = ord('z') - ord(y) + ord(x) - ord('a') + 1\n",
    "            ans += min(a, b) + 1\n",
    "            prev = c\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        n=len(word)\n",
    "        cut=\"a\"\n",
    "        for char in word:\n",
    "            lence=abs(ord(char)-ord(cut))\n",
    "            n +=min(lence,26-lence)\n",
    "            cut=char\n",
    "        return n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        cur = 'a'\n",
    "        res = 0\n",
    "        for c in word:\n",
    "            if c == cur:\n",
    "                res += 1\n",
    "            else:\n",
    "                diff = abs(ord(c) - ord(cur))\n",
    "                res += min(diff, 26 - diff) + 1\n",
    "                cur = c\n",
    "        return res\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        cur = 'a'\n",
    "        res = len(word)\n",
    "        for c in word:\n",
    "            diff = abs(ord(c) - ord(cur))\n",
    "            res += min(diff, 26 - diff)\n",
    "            cur = c\n",
    "        return res\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        n=len(word)\n",
    "        a=ord('a')\n",
    "        ans=n\n",
    "        for i in range(n):\n",
    "            ans+=min(abs(ord(word[i])-a),26-abs(ord(word[i])-a))\n",
    "            a=ord(word[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        curr=ord('a')\n",
    "        ans=0\n",
    "        for s in word:\n",
    "            t = abs(ord(s)-curr)\n",
    "            if t > 13:\n",
    "                t = 26 - t\n",
    "            ans = ans + t + 1        \n",
    "            curr=ord(s)\n",
    "        return ans            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans=0\n",
    "        x=abs(ord(word[0])-ord(\"a\"))\n",
    "        ans+=min(x,26-x)\n",
    "        j=word[0]\n",
    "        for i in word[1::]:\n",
    "            num=abs(ord(i)-ord(j))\n",
    "            ans+=min(num,26-num)\n",
    "            ans+=1\n",
    "            j=i\n",
    "        return ans+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        res = len(word)\n",
    "        cur = \"a\"\n",
    "        for char in word: \n",
    "            val = abs(ord(char) - ord(cur))\n",
    "            res += min(val, 26 - val)\n",
    "            cur = char\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        prev = 0\n",
    "        res = 0\n",
    "        for ch in word:\n",
    "            curr = ord(ch)-ord('a')\n",
    "            res += 1+min(abs(curr-prev),26-abs(curr-prev))\n",
    "            prev = curr\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans = len(word)\n",
    "        now = 'a'\n",
    "        for c in word:\n",
    "            v = abs(ord(c) - ord(now))\n",
    "            ans += min(v, 26-v)\n",
    "            now = c\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans = len(word)\n",
    "        last = 97\n",
    "        for c in map(ord, word):\n",
    "            ans += min(dis := abs(c - last), 26 - dis)\n",
    "            last = c\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans = len(word)\n",
    "        now = 'a'\n",
    "        for c in word:\n",
    "            v = abs(ord(c) - ord(now))\n",
    "            ans += min(v, 26-v)\n",
    "            now = c\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        dic = {chr(i):i for i in range(97,123)}\n",
    "        w = 'a' + word\n",
    "        ans = 0\n",
    "        for i in range(len(w) - 1):\n",
    "            ans += min(abs(dic[w[i]] - dic[w[i+1]]),26 - abs(dic[w[i]] - dic[w[i+1]])) + 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        start=\"a\"\n",
    "        ans=0\n",
    "        for ch in word:\n",
    "            cnt=abs(ord(ch)-ord(start))\n",
    "            ans+=cnt if cnt<=13 else 26-cnt\n",
    "            ans+=1\n",
    "            start=ch \n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans=0\n",
    "        x=abs(ord(word[0])-ord(\"a\"))\n",
    "        ans+=min(x,26-x)\n",
    "        j=word[0]\n",
    "        for i in word[1::]:\n",
    "            num=abs(ord(i)-ord(j))\n",
    "            ans+=min(num,26-num)\n",
    "            ans+=1\n",
    "            j=i\n",
    "        return ans+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        d = {}\n",
    "        s = \"abcdefghijklmnopqrstuvwxyz\"\n",
    "        for i in range(1,27):\n",
    "            d[s[i-1]] = i\n",
    "        \n",
    "        ans = 0\n",
    "        pos = 1\n",
    "        a = d[word[0]]\n",
    "        if a - 1 > 13:\n",
    "            ans += (26-a+1)\n",
    "        else:\n",
    "            ans += (a-1)\n",
    "        for i in range(1, len(word)):\n",
    "            a = d[word[i-1]]\n",
    "            b = d[word[i]]\n",
    "            if abs(a-b) > 13:\n",
    "                aa = 0\n",
    "                if a >= b:\n",
    "                    aa = (26-a) + b\n",
    "                else:\n",
    "                    aa = a + (26-b)\n",
    "                ans += aa\n",
    "            else:\n",
    "                ans += abs(a-b)\n",
    "\n",
    "            pos = b\n",
    "            ans += 1\n",
    "            print(ans)\n",
    "        ans += 1\n",
    "        return ans\n",
    "            \n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans = len(word)\n",
    "        now = 'a'\n",
    "        for c in word:\n",
    "            v = abs(ord(c) - ord(now))\n",
    "            ans += min(v, 26-v)\n",
    "            now = c\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        s_time = 0\n",
    "        word_list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']\n",
    "        for w in word:\n",
    "            w_i = word_list.index(w)\n",
    "            w_ri = 26 - w_i\n",
    "            m_step = min(w_i,w_ri)\n",
    "            m_flag = w_i - w_ri\n",
    "            while m_step > 0:\n",
    "                if m_flag <= 0:\n",
    "                    word_list.append(word_list.pop(0))\n",
    "                    s_time += 1\n",
    "                else:\n",
    "                    word_list.insert(0,word_list.pop(-1))\n",
    "                    s_time += 1\n",
    "                m_step -= 1\n",
    "            s_time += 1\n",
    "        return s_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        # 上次指针的位置\n",
    "        last=0\n",
    "        # 记录耗时\n",
    "        # 指针当前的位置，每次移动后，last 要移动到当前位置来\n",
    "        cur=0\n",
    "        res=0\n",
    "        for ch in word:\n",
    "            cur=ord(ch)-ord('a')\n",
    "            time=1+min(abs(cur-last),26-abs(cur-last))\n",
    "            res+=time\n",
    "            last=cur\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans=0\n",
    "        ans+=min(ord(word[0])-ord('a'),26-(ord(word[0])-ord('a')))+1\n",
    "        for i in range(1,len(word)):\n",
    "            tmp=abs(ord(word[i])-ord(word[i-1]))\n",
    "            tmp1=min(tmp,26-tmp)+1\n",
    "            ans+=tmp1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        s_time = 0\n",
    "        word_list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']\n",
    "        for w in word:\n",
    "            w_i = word_list.index(w)\n",
    "            w_ri = 26 - w_i\n",
    "            m_step = min(w_i,w_ri)\n",
    "            m_flag = w_i - w_ri\n",
    "\n",
    "            if m_flag <= 0:\n",
    "                word_list = word_list[m_step:] + word_list[:m_step]\n",
    "                s_time += m_step\n",
    "            else:\n",
    "                word_list = word_list[-m_step:] + word_list[:-m_step]\n",
    "                s_time += m_step\n",
    "            # for _ in range(m_step):\n",
    "            #     if m_flag <= 0:\n",
    "            #         word_list.append(word_list.pop(0))\n",
    "            #         s_time += 1\n",
    "            #     else:\n",
    "            #         word_list.insert(0,word_list.pop(-1))\n",
    "            #         s_time += 1\n",
    "            #     m_step -= 1\n",
    "            s_time += 1\n",
    "        return s_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans = len(word)\n",
    "        last = 97\n",
    "        for c in map(ord, word):\n",
    "            dis = abs(c - last)\n",
    "            ans += min(26 - dis, dis)\n",
    "            last = c\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        # 初始化指针位置和所需总时间\n",
    "        pointer = 'a'\n",
    "        time = 0\n",
    "\n",
    "        for char in word:\n",
    "            # 计算顺时针和逆时针两种方向所需的时间，并选择较小的一个\n",
    "            clockwise = abs(ord(char) - ord(pointer))\n",
    "            anticlockwise = 26 - clockwise\n",
    "            time += min(clockwise, anticlockwise)\n",
    "\n",
    "            # 加上键入字符所需的1秒\n",
    "            time += 1\n",
    "\n",
    "            # 更新指针位置\n",
    "            pointer = char\n",
    "\n",
    "        return time\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        n = len(word)\n",
    "        res = n + min(ord(word[0]) - 97, 26 - (ord(word[0]) - 97))\n",
    "        for i in range(n-1):\n",
    "            a, b = max(word[i:i+2]), min(word[i:i+2])\n",
    "            res += min(ord(a)-ord(b), ord(b)-ord(a)+26)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        s_time = 0\n",
    "        word_list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']\n",
    "        for w in word:\n",
    "            w_i = word_list.index(w)\n",
    "            w_ri = 26 - w_i\n",
    "            m_step = min(w_i,w_ri)\n",
    "            m_flag = w_i - w_ri\n",
    "            while m_step > 0:\n",
    "                if m_flag <= 0:\n",
    "                    word_list.append(word_list.pop(0))\n",
    "                    s_time += 1\n",
    "                else:\n",
    "                    word_list.insert(0,word_list.pop(-1))\n",
    "                    s_time += 1\n",
    "                m_step -= 1\n",
    "            s_time += 1\n",
    "        return s_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        s_time = 0\n",
    "        word_list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']\n",
    "        for w in word:\n",
    "            w_i = word_list.index(w)\n",
    "            w_ri = 26 - w_i\n",
    "            m_step = min(w_i,w_ri)\n",
    "            m_flag = w_i - w_ri\n",
    "            for _ in range(m_step):\n",
    "                if m_flag <= 0:\n",
    "                    word_list.append(word_list.pop(0))\n",
    "                    s_time += 1\n",
    "                else:\n",
    "                    word_list.insert(0,word_list.pop(-1))\n",
    "                    s_time += 1\n",
    "                m_step -= 1\n",
    "            s_time += 1\n",
    "        return s_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        last = 'a'\n",
    "        ret = 0\n",
    "        for x in word:\n",
    "            ret += min((ord(x) - ord(last) + 26) % 26, (ord(last) - ord(x) + 26) % 26) + 1\n",
    "            last = x\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        lst = [ord(c) - 97 for c in word]\n",
    "\n",
    "        pre = lst[0]\n",
    "        ans = min(pre, 26 - pre) + 1\n",
    "        for c in lst[1:]:\n",
    "            if c > pre:\n",
    "                t = min(c - pre, pre + 26 - c) + 1\n",
    "            else:\n",
    "                t = min(pre - c, c + 26 - pre) + 1\n",
    "            pre = c\n",
    "            ans += t\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "'''class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        t2 = abs(ord(word[0]) - ord('a'))\n",
    "        t = 0\n",
    "        if  t2 <= 12:\n",
    "            t = t + 1 + t2\n",
    "        else:\n",
    "            t = t + 27 - t2\n",
    "        for i in range(1,len(word)):\n",
    "            t2 = abs(ord(word[i]) - ord(word[i-1]))\n",
    "            if  t2 <= 12:\n",
    "                t = t + 1 + t2\n",
    "            else:\n",
    "                t = t + 27 -t2\n",
    "        return t'''\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        prev = 0\n",
    "        res = 0   # 当前位置\n",
    "        for ch in word:\n",
    "            # 计算键入每个字符的最小耗时并更新当前位置\n",
    "            curr = ord(ch) - ord('a')\n",
    "            res += 1 + min(abs(curr - prev), 26 - abs(curr - prev))\n",
    "            prev = curr\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        s_time = 0\n",
    "        word_list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']\n",
    "        for w in word:\n",
    "            w_i = word_list.index(w)\n",
    "            w_ri = 26 - w_i\n",
    "            m_step = min(w_i,w_ri)\n",
    "            m_flag = w_i - w_ri\n",
    "            if m_flag <= 0:\n",
    "                word_list = word_list[m_step:] + word_list[:m_step]\n",
    "                s_time += m_step\n",
    "            else:\n",
    "                word_list = word_list[-m_step:] + word_list[:-m_step]\n",
    "                s_time += m_step\n",
    "            s_time += 1\n",
    "        return s_time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        pointer = ord('a')\n",
    "        count = len(word)\n",
    "        for w in word:\n",
    "            count += min((pointer-ord(w))%26,(ord(w)-pointer)%26)\n",
    "            pointer = ord(w)\n",
    "        return count\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "\n",
    "\n",
    "        res = 0\n",
    "        cur = 0\n",
    "\n",
    "        for c in word:\n",
    "\n",
    "            res += min(abs(cur - (ord(c) - ord('a'))), abs(26 - abs(cur - ord(c) + ord('a'))))  + 1\n",
    "            cur = ord(c) - ord('a')\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        counter = 0\n",
    "        old = 'a'\n",
    "        for w in word:\n",
    "            dis = abs(ord(old) - ord(w))\n",
    "            counter += min(dis, 26-dis) + 1\n",
    "            old = w\n",
    "        return counter\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        res = len(word)\n",
    "        pre = 'a'\n",
    "        cur = word[0]\n",
    "        n = len(word)\n",
    "        for i in range(n):\n",
    "            t = abs(ord(cur) - ord(pre))\n",
    "            print(cur, pre)\n",
    "            t = min(t, 26-t)\n",
    "            res += t\n",
    "            if i + 1 < n:\n",
    "                pre, cur = word[i], word[i+1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        s=min(26-abs((ord(word[0])-ord('a'))),abs(ord(word[0])-ord('a')))\n",
    "        for i in range(1,len(word)):\n",
    "            s+=min(26-abs((ord(word[i])-ord(word[i-1]))),abs(ord(word[i])-ord(word[i-1])))\n",
    "        return s+len(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        m,n=len(word),'a'\n",
    "        for i in word:\n",
    "            j=abs(ord(i)-ord(n))\n",
    "            m+=min(j,26-j)\n",
    "            n=i\n",
    "        return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        count=0\n",
    "        cur='a'\n",
    "        for i in word:            \n",
    "            if abs(ord(i)-ord(cur))<13:\n",
    "                count+=abs(ord(i)-ord(cur))+1\n",
    "            else:\n",
    "                count+=26-abs(ord(i)-ord(cur))+1\n",
    "            cur=i\n",
    "        return count\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans = n = len(word)\n",
    "        pre_alpha = 'a'\n",
    "        for i in range(n):\n",
    "            dis = abs(ord(word[i]) - ord(pre_alpha))\n",
    "            ans += min(dis, 26 - dis)\n",
    "            pre_alpha = word[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans = len(word)\n",
    "        last = 97\n",
    "        for c in map(ord, word):\n",
    "            ans += min(dis := abs(c - last), 26 - dis)\n",
    "            last = c\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        ans = 0\n",
    "        cw = 'a'\n",
    "        for w in word:\n",
    "            ans += 1\n",
    "            if w != cw:\n",
    "                c = abs(ord(w) - ord(cw))\n",
    "                ans += c if c <= 13 else 26 - c\n",
    "                cw = w\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        res = len(word)\n",
    "        cur = \"a\"\n",
    "        for char in word: \n",
    "            val = abs(ord(char) - ord(cur))\n",
    "            res += min(val, 26 - val)\n",
    "            cur = char\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        初始指针 = 0\n",
    "        使用最少时间 = len(word)\n",
    "        for i in word:\n",
    "            输入 = ord(i)-97\n",
    "            符值 = abs(输入 - 初始指针)\n",
    "            使用最少时间 += min(符值, 26-符值)\n",
    "            初始指针 = 输入\n",
    "        return 使用最少时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        res = 0\n",
    "        start = \"a\"\n",
    "        for i in word:\n",
    "            distance = abs(ord(i)-ord(start))\n",
    "            res += min(distance,26-distance)+1\n",
    "            start = i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        count=min(abs(ord(word[0])-ord('a')),26-abs(ord(word[0])-ord('a')))\n",
    "        if len(word)==1: return count+1\n",
    "        for i in range(1,len(word)):\n",
    "            count+=min(abs(ord(word[i])-ord(word[i-1])),26-abs(ord(word[i])-ord(word[i-1])))\n",
    "        count+=len(word)\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        prev = 0\n",
    "        res = 0   # 当前位置\n",
    "        for ch in word:\n",
    "            # 计算键入每个字符的最小耗时并更新当前位置\n",
    "            curr = ord(ch) - ord('a')\n",
    "            res += 1 + min(abs(curr - prev), 26 - abs(curr - prev))\n",
    "            prev = curr\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minTimeToType(self, word: str) -> int:\n",
    "        curr=ord('a')\n",
    "        ans=0\n",
    "        for s in word:\n",
    "            t = abs(ord(s)-curr)\n",
    "            if t > 13:\n",
    "                t = 26 - t\n",
    "            ans = ans + t + 1        \n",
    "            curr=ord(s)\n",
    "        return ans            "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
